package com.ccit.equip.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ccit.common.client.EmployeeClient;
import com.ccit.common.client.MoneyClient;
import com.ccit.common.exception.GymException;
import com.ccit.common.result.Result;
import com.ccit.common.utils.PageUtil;
import com.ccit.common.utils.RedisUtils;
import com.ccit.common.utils.UploadUtil;
import com.ccit.equip.mapper.OverhaulMapper;
import com.ccit.equip.service.OverhaulService;
import com.ccit.model.Employee;
import com.ccit.model.Overhaul;
import com.ccit.model.vo.Overhaul.OverhaulResultVo;
import com.ccit.model.vo.Overhaul.OverhaulVo;
import com.ccit.model.vo.money.MoneyVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@Api(tags = "器材管理")
@RestController
@RequestMapping("/overhaul")
public class OverhaulController {

    @Autowired
    private OverhaulService overhaulService;

    @Autowired
    private EmployeeClient employeeClient;

    @Autowired
    private MoneyClient moneyClient;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private UploadUtil uploadUtil;

    @Autowired
    OverhaulMapper overhaulMapper;


    @ApiOperation(value = "查询维修记录")
    @GetMapping("/getRecordByPage/{current}/{size}")
    public Result getRecord(@PathVariable Integer current,
                            @PathVariable Integer size) {
        String key = "repairPage:" + current + ":" + size;
        boolean hasKey = redisUtils.hasKey(key);
        Page<Overhaul> page;

        if (hasKey) {
            page = (Page<Overhaul>)redisUtils.get(key);
        } else {
            Page<Overhaul> repairPage = new Page<>(current, size);
            page = overhaulService.page(repairPage);
            redisUtils.set(key, page);
        }
        return Result.ok(page);
//        QueryWrapper<Overhaul> wrapper = new QueryWrapper<>();
//        wrapper.eq("employee_id", employeeId);
//
//        QueryWrapper<Overhaul> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("c.del_flag",0)
//                .like(StringUtil.isNotBlank(Overhaul.()),"c.company_fullname",customerManager.getCompanyFullname())
//                .apply("c.sale_id = s.sale_id")
//                .orderByDesc("c.create_time");

//        return null;
    }

    @ApiOperation(value = "查询所有维修记录")
    @GetMapping("/listAll")
    public Result listAll() {
        List<Overhaul> list;
        String key = "allRepair";
        boolean hasKey = redisUtils.hasKey(key);
        if (hasKey) {
            list = (List<Overhaul> )redisUtils.get(key);
        } else {
            list = overhaulService.listAllOverhaulRecord();
            redisUtils.set(key,list);
        }

        return Result.ok(list);
    }

    @ApiOperation(value = "条件查询")
    @PostMapping("/getByDynamic/{current}/{size}")
    public Result getByDynamic(
            @RequestBody OverhaulVo overhaulVo,
            @PathVariable Integer current,
            @PathVariable Integer size) {
        List<OverhaulResultVo> resultVos;
        String key = "overhaulList";
        if (overhaulVo.getEmployeeId()!=null){
            key = key + ":" + overhaulVo.getEmployeeId();
        }
        if (overhaulVo.getName()!=null){
            key = key +":" +overhaulVo.getName();
        }
        boolean hasKey = redisUtils.hasKey(key);
        if (hasKey){
            resultVos = (List<OverhaulResultVo>)redisUtils.get(key);
        }else {
            QueryWrapper<Overhaul> wrapper = new QueryWrapper<Overhaul>();

            if (overhaulVo.getName() != null){
                if (overhaulVo.getEmployeeId() != null){
                    wrapper.lambda()
                            .eq(Overhaul::getEmployeeId, overhaulVo.getEmployeeId())
                            .eq(Overhaul::getName,overhaulVo.getName())
                            .orderByDesc(Overhaul::getTime);
                }else {
                    wrapper.lambda()
                            .eq(Overhaul::getName,overhaulVo.getName())
                            .orderByDesc(Overhaul::getTime);
                }
            }else if (overhaulVo.getEmployeeId() != null){
                wrapper.lambda()
                        .eq(Overhaul::getEmployeeId, overhaulVo.getEmployeeId())
                        .orderByDesc(Overhaul::getTime);
            }else {
                wrapper.lambda()
                        .orderByDesc(Overhaul::getTime);
            }
            List<OverhaulResultVo> resultVoss = new ArrayList<>();
            List<Overhaul> list = overhaulService.list(wrapper);
            list.forEach(overhaul -> {
                OverhaulResultVo resultVo = new OverhaulResultVo();
                Employee employee = employeeClient.getEmployeeById(overhaul.getEmployeeId());
                resultVo.setEmployee(employee);
                resultVo.setCount(overhaul.getCount());
                resultVo.setId(overhaul.getId());
                resultVo.setName(overhaul.getName());
                resultVo.setPrice(overhaul.getPrice());
                resultVo.setTime(overhaul.getTime());
                resultVoss.add(resultVo);
            });
            resultVos = resultVoss ;
            redisUtils.set(key,resultVos);
        }

        Page page = PageUtil.getPages(current, size, resultVos);

        return Result.ok(page);
    }


    @ApiOperation(value = "添加维修记录")
    @PostMapping("/addRepairRecord")
    public Result addRepairRecord(@RequestBody Overhaul overhaulVo) {
        Overhaul overhaul = new Overhaul();
        overhaul.setCount(overhaulVo.getCount());
        overhaul.setEmployeeId(overhaulVo.getEmployeeId());
        overhaul.setName(overhaulVo.getName());
        overhaul.setPrice(overhaulVo.getPrice());

        MoneyVo moneyVo = new MoneyVo();
        moneyVo.setMoneyType("支出");
        moneyVo.setTypeName("维修");
        moneyVo.setAmout(overhaul.getPrice());
        boolean b = moneyClient.apiSaveMoney(moneyVo);
        if (!b){
            throw new GymException(301,"添加维修资金记录失败");
        }
        boolean result = overhaulService.save(overhaul);
        if (result) {
            redisUtils.deleteRedisKeys("overhaulList");
            redisUtils.deleteRedisKeys("repairPage");
            redisUtils.deleteRedisKeys("allRepair");
            return Result.ok("添加成功");
        } else {
            return Result.fail().message("添加失败");
        }
    }
}
